package fr.insta.browser.view.menu;

import fr.insta.webview.object.IWebObject;
import fr.insta.webview.service.ServiceWebObject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * <p>JMenuBarWebView class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public final class JMenuBarWebView extends JMenuBar implements IJMenuBarWebView {

    private static final long serialVersionUID = 1L;
    private JMenu menuBookmark;
    private JMenu menuHistory;
    private IJMenuBarAction menuBarAction;
	private List<IWebObject> listBookmarks, listHistory;
    
    /**
     * <p>Constructor for JMenuBarWebView.</p>
     *
     * @param _menuBarAction a {@link fr.insta.browser.view.menu.IJMenuBarAction} object.
     */
    public JMenuBarWebView(IJMenuBarAction _menuBarAction) {
        menuBarAction = _menuBarAction;
        
        generateMenu();
        
        listBookmarks = ServiceWebObject.INSTANCE.unserializeBookmarks();
        listHistory = ServiceWebObject.INSTANCE.unserializeHistory();
        
        refreshHistory();
        refreshBookmark();
    }
    
    private void generateMenu() {
        menuBookmark = new JMenu("Marques Pages");
        menuBookmark.setMnemonic(KeyEvent.VK_M);
        
        menuHistory = new JMenu("Historique");
        menuHistory.setMnemonic(KeyEvent.VK_H);

        JMenu menuView = new JMenu("Afficher");

        JMenuItem zoomIn = new JMenuItem("Zoom Avant (+)", KeyEvent.VK_PLUS);
        JMenuItem zoomOut = new JMenuItem("Zoom Arrière (-)", KeyEvent.VK_MINUS);
        zoomIn.addActionListener(new OnClickListenerItem(StateOnCLick.ZOOM_IN));
        zoomOut.addActionListener(new OnClickListenerItem(StateOnCLick.ZOOM_OUT));

        JMenuItem showSource = new JMenuItem("Afficher le code source");
        showSource.addActionListener(new OnClickListenerItem(StateOnCLick.SHOW_SOURCE));

        menuView.add(zoomIn);
        menuView.add(zoomOut);
        menuView.addSeparator();
        menuView.add(showSource);

        JMenu menuOutils = new JMenu("Outils");

        JMenuItem change_home = new JMenuItem("Préférences");
        change_home.addActionListener(new OnClickListenerItem(StateOnCLick.SHOW_PREFS));
        menuOutils.add(change_home);

        this.add(menuBookmark);
        this.add(menuHistory);
        this.add(menuView);
        this.add(menuOutils);
    }

    /** {@inheritDoc} */
    @Override
    public void refreshHistory() {
    		
    	ArrayList<IWebObject> listReversed = new ArrayList<>(listHistory);
    	Collections.reverse(listReversed);
    	
    	menuHistory.removeAll();
    	
    	JMenuItem itemShowHistory = new JMenuItem("Afficher historique", KeyEvent.VK_A);
        itemShowHistory.addActionListener(new OnClickListenerItem(StateOnCLick.SHOW_HISTORY));
        menuHistory.add(itemShowHistory);
    	
        if (!listHistory.isEmpty()) {
            menuHistory.addSeparator();
        }

        for (IWebObject history : listReversed) {
            JMenuItem menuItem = new JMenuItem();
            menuItem.putClientProperty("webObject", history);
            menuItem.setText(history.getTitle());
            
            menuHistory.add(menuItem);
        }
    }
    
    

    /** {@inheritDoc} */
    @Override
    public void refreshBookmark() {
    	
    	menuBookmark.removeAll();
    	
    	JMenuItem itemBookmarkPage = new JMenuItem("Marquer cette page", KeyEvent.VK_A);
    	itemBookmarkPage.addActionListener(new OnClickListenerItem(StateOnCLick.ADD_BOOKMARK));
    	menuBookmark.add(itemBookmarkPage);
    	
    	JMenuItem itemShowBookmark= new JMenuItem("Voir les marques pages", KeyEvent.VK_V);
    	itemShowBookmark.addActionListener(new OnClickListenerItem(StateOnCLick.SHOW_BOOKMARK));
        menuBookmark.add(itemShowBookmark);
    	
        if (!listBookmarks.isEmpty()) {
            menuBookmark.addSeparator();
        }

        for (IWebObject bookmark : listBookmarks) {
            JMenuItem menuItem = new JMenuItem();

            menuItem.putClientProperty("webObject", bookmark);

            menuItem.setText(bookmark.getTitle());
            menuItem.addActionListener(new OnClickListenerItem(StateOnCLick.GOTO));

            menuBookmark.add(menuItem);
        }
    }
    
    enum StateOnCLick {
        SHOW_HISTORY, ADD_BOOKMARK, SHOW_BOOKMARK, GOTO, ZOOM_IN, ZOOM_OUT, SHOW_SOURCE, SHOW_PREFS;
    }

    class OnClickListenerItem implements ActionListener {

        private StateOnCLick stateOnClick;

        public OnClickListenerItem(StateOnCLick _stateOnClick) {
            stateOnClick = _stateOnClick;
        }

        @Override
        public void actionPerformed(ActionEvent _actionEvent) {
            JComponent component = (JComponent) _actionEvent.getSource();
            
            switch (stateOnClick) {
                case ADD_BOOKMARK:
                    menuBarAction.onClickAddBookmark();
                    break;
                case SHOW_HISTORY:
                    menuBarAction.onClickShowHistory();
                    break;
                case GOTO:
                    IWebObject history = (IWebObject) component.getClientProperty("webObject");
                    menuBarAction.onClickGotoWebObject(history);
                    break;
                case ZOOM_IN:
                    menuBarAction.onClickZoomIn();
                    break;
                case ZOOM_OUT:
                    menuBarAction.onClickZoomOut();
                    break;
                case SHOW_SOURCE:
                	menuBarAction.onClickShowSource();
                	break;
                case SHOW_BOOKMARK:
                    menuBarAction.onClickShowBookmark();
                    break;
                case SHOW_PREFS:
                	menuBarAction.onClickShowPrefs();
                	break;
                default:
                    break;
            }
        }
    }

	/** {@inheritDoc} */
	@Override
	public void refresh()
	{
		refreshBookmark();
		refreshHistory();
	}
	
	/** {@inheritDoc} */
	@Override
	public List<IWebObject> getListHistory()
	{
		return listHistory;
	}

	/** {@inheritDoc} */
	@Override
	public List<IWebObject> getListBookmark()
	{
		return listBookmarks;
	}
}
